<template>
  <div id="windowRpmKojiParseManage" class="windowEditorTab">
    <el-input v-model="$store.state.obj_setting_map.rpm_repo_parse_link" disabled placeholder="Rpm Server"
              style="margin-bottom: 8px"/>
    <el-input v-model="rpm_name" placeholder="输入软件包名称" @keydown.enter="get_rpm_repo_info">
      <template #append>
        <el-button @click="get_rpm_repo_info" :loading="koji_parse_loading">
          <el-icon>
            <Search/>
          </el-icon>
        </el-button>
      </template>
    </el-input>
    <el-radio-group v-model="show_type" style="margin-top: 5px" @change="rpm_repo_info_filter">
      <el-radio-button label="显示全部"/>
      <el-radio-button label="仅显示最新"/>
      <el-radio-button label="已发布"/>
      <el-radio-button label="koji"/>
      <el-radio-button label="分类显示"/>
    </el-radio-group>
    <div id="rpm_version_show" :style="'height:' + (height - 140) + 'px'">
      <el-timeline v-loading="koji_parse_loading" v-if="show_type!=='分类显示'">
        <el-timeline-item v-for="(repo, i) in koji_parse_filter" :key="i" :timestamp="repo.tag_arch" placement="top">
          <el-link :href="repo.repo_link + '/Packages/'" target="_blank" type="info">>>>
            跳转到仓库（{{ repo.repo_link + '/Packages/' }}）
          </el-link>
          <ol style="margin-left: -22px">
            <li v-for="(repo_rpm_one, r_i) in repo.rpm_src_list" :key="r_i">
              <el-link :href="repo.repo_link + '/Packages/'" target="_blank">{{ repo_rpm_one }}</el-link>
            </li>
          </ol>
        </el-timeline-item>
      </el-timeline>

    </div>

  </div>
</template>

<script>
import axios from 'axios'
import check from "@/utils/check";
import {ElNotification} from 'element-plus'
// import windowCodeMirrorComponent from "@/components/windowCodeMirrorComponent";
import {Search} from '@element-plus/icons-vue'

export default {
  name: "windowRpmKojiParseManage",
  props: {
    sessionToken: String,
    codeName: String,
    height: Number
  },
  components: {Search},
  data() {
    return {
      rpm_name: "",
      show_type: "显示全部",
      koji_parse_loading: false,
      koji_parse: [],
      koji_parse_filter: [],
      rpm_repo_parse_link: "",
      form_function_choice: {},
    }
  },
  methods: {
    do_copy(copy_str) {
      this.$copyText(copy_str).then(function () {
        ElNotification({title: '复制成功', type: 'success',})
      }, function () {
        ElNotification({title: '复制失败', type: 'error',})
      })
    },
    rpm_repo_info_filter(filter_type) {
      if (filter_type === "显示全部") {
        this.koji_parse_filter = check.clone(this.koji_parse)
      } else if (filter_type === "仅显示最新") {
        this.koji_parse_filter = check.clone(this.koji_parse)
        for (let i = 0; i < this.koji_parse_filter.length; i++) {
          this.koji_parse_filter[i].rpm_src_list = this.koji_parse_filter[i].rpm_src_list.slice(-1)
        }
      } else if (filter_type === "已发布") {
        this.koji_parse_filter = []
        for (let i = 0; i < this.koji_parse.length; i++) {
          if (this.koji_parse[i].tag_arch.startsWith("【已发布】"))
            this.koji_parse_filter.push(this.koji_parse[i])
        }
      } else if (filter_type === "koji") {
        this.koji_parse_filter = []
        for (let i = 0; i < this.koji_parse.length; i++) {
          if (this.koji_parse[i].tag_arch.startsWith("【Koji"))
            this.koji_parse_filter.push(this.koji_parse[i])
        }
      }
    },
    get_rpm_repo_info() {
      if (this.rpm_name === "") return
      let obj_this = this
      let data = {
        "session_token": obj_this.sessionToken,
        "rpm_name": this.rpm_name
      }
      obj_this.koji_parse_loading = true
      obj_this.rpm_repo_parse_link = obj_this.$store.state.obj_setting_map.rpm_repo_parse_link
      let repo_parse_link = obj_this.rpm_repo_parse_link + "/search_rpm_name"
      axios.post(repo_parse_link, data).then(function (response) {
        obj_this.koji_parse = response.data.data
        // console.log(response.data.data)
        obj_this.rpm_repo_info_filter(obj_this.show_type)
        obj_this.koji_parse_loading = false
      }).catch(function (error) { // 请求失败处理
        console.log(error);
      });
    }
    ,
    show_all_spec(file_str) {
      this.$store.state.obj_windowEditorManage.addTab_code_mirror_page_out(file_str)
    }
  },
  mounted() {
  }
}
</script>

<style scoped>
#rpm_version_show {
  margin-top: 10px;
  overflow: auto;
}
</style>
